package org.grc.app.z_project.z_framework.proxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CGLibDynamicProxy implements MethodInterceptor{

	// Singleton
	private static CGLibDynamicProxy instance = new CGLibDynamicProxy();
	
	// 私有构造器
	private CGLibDynamicProxy() {
	}
	
	public static CGLibDynamicProxy getInstance () {
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getProxy (Class<T> clazz) {
		return (T) Enhancer.create(clazz, this);
	}
	
	@Override
	public Object intercept (Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		before();
		Object result = proxy.invokeSuper(target, args);
		after();
		return result;
	}

	private void before () {
		System.out.println("CGLib Proxy Before");
	}
	
	private void after () {
		System.out.println("CGLib Proxy After");
	}
}
